package de.szut.ita13.sommerfeld.loganalyser;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class ApacheLog {

	private String title;
	private File logfile;
	private ArrayList stats;
	public static File statFolder;
	
	
	public ApacheLog(String title, File logfile, ArrayList stats) {
		statFolder = new File("statFolder");
		if(!statFolder.exists())
			statFolder.mkdir();
		this.title = title;
		this.logfile = logfile;
		this.stats = stats;
	}
	
	public ApacheLog() {}

	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public File getLogfile() {
		return logfile;
	}

	public void setLogfile(File logfile) {
		this.logfile = logfile;
	}

	public ArrayList getStats() {
		return stats;
	}

	public void setStats(ArrayList stats) {
		this.stats = stats;
	}
	
	public String writeStatsToFile() {
		File statsFile = new File(statFolder.getAbsolutePath() + "/" + "stat_" + title);
		try {
			if(!statsFile.exists())
				statsFile.createNewFile();
			BufferedWriter bufferedwriter = new BufferedWriter(new FileWriter(statsFile));
			for(int i = 0; i < stats.size(); i++) {
				bufferedwriter.write(GUI.LABELNAME[0][i] + "\t" + stats.get(i));
				bufferedwriter.write(System.getProperty("line.separator"));
			}
			bufferedwriter.close();
			return "stat_" + title + " successfully created!";
		} catch (IOException e) {
			e.printStackTrace();
			return "stat_" + title + " creation failed!";
		}
	}
}
